package com.lovo.roomProject.student.dao.impl;

import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.ClaszDO;
import com.lovo.roomProject.pojo.RoomDO;
import com.lovo.roomProject.pojo.StudentDO;
import com.lovo.roomProject.student.dao.StudentDAO;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class StudentDAOImpl extends BaseDAO implements StudentDAO {

    @Override
    public List listStudent() {
        this.getConnection();
        String sql = "SELECT s.*,c.*,r.* FROM " +
                "t_student s LEFT JOIN t_class c on s.s_c_id = c.c_id" +
                " LEFT JOIN t_room r on s.s_r_id=r.r_id ";
        List list =new ArrayList();
        try {
            statement =con.prepareStatement(sql);
            rs =statement.executeQuery();
            while (rs.next()){
                StudentDO studentDO=new StudentDO();
                studentDO.setId(rs.getInt("s_id"));
                studentDO.setName(rs.getString("s_name"));
                studentDO.setGender(rs.getString("s_gender"));
                studentDO.setPhone(rs.getString("s_phone"));
                studentDO.setDate(LocalDate.parse(rs.getString("s_date")));
                studentDO.setHead(rs.getString("s_head"));
                /**班级*/
                ClaszDO claszDO =new ClaszDO();
                claszDO.setId(rs.getInt("c_id"));
                claszDO.setName(rs.getString("c_name"));
                studentDO.setClaszDO(claszDO);
                /**房间*/
                RoomDO roomDO =new RoomDO();
                roomDO.setId(rs.getInt("r_id"));
                roomDO.setAddr(rs.getString("r_addr"));
                studentDO.setRoomDO(roomDO);

                list.add(studentDO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,statement,rs);
        }
        return list;
    }
}
